package bdddaosimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import bdd.beans.Contratquadriennal;
import bdd.beans.DBLink;
import bdd.beans.Ec;
import bdd.beans.Ue;
import bdddaos.DAOEc;
import bdddaos.DAOEc;
import bdddaos.DAOUe;
import bdddaos.DAOUe;

//OK
public class DAOEcImpl implements DAOEc {
	private DBLink dblink;
	
	public DAOEcImpl() {
		this.dblink = new DBLink();
	}
	
	@Override
	public Ec save(Ec o) {
		try {
			this.dblink.connexion();
			String r = "INSERT INTO EC (NUMUE, LIBELLE, COEF)" +
			" VALUES ("+o.getUe().getNumue()+", '"+ o.getLibelle() +"', "+o.getCoef()+")";
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
			DAOOutils outil = new DAOOutils();
			o.setNumec(outil.findMaxId("EC", "NUMEC"));
		}
		catch(SQLException se) {
			se.printStackTrace();
		}
		finally{
			this.dblink.deconnexion();
		}
		return o;
	}

	@Override
	public Ec find(Integer id) {
		try {
			this.dblink.connexion();
			String r = "SELECT * FROM EC WHERE NUMEC = " + id;
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			ResultSet rs = this.dblink.getPst().executeQuery();
			if(rs.next()) {
				Ec retour = new Ec();
				retour.setNumec(id);
				retour.setLibelle(rs.getString("LIBELLE"));
				retour.setCoef(rs.getDouble("COEF"));
				
				DAOUe daoUe = new DAOUeImpl();
				Ue ue = daoUe.find(rs.getInt("NUMUE"));
				retour.setUe(ue);
				
				retour.setCreneaux(null);
				retour.setJntexamenpromotionecs(null);
				retour.setJnttypeecenseignants(null);
				retour.setJnttypeecs(null);
				
				this.dblink.getPst().close();
				this.dblink.deconnexion();
				
				return retour;
			}		
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return null;
	}

	@Override
	public List<Ec> findAll() {
		List<Ec> Lstretour = new ArrayList<Ec>();
		try {
			this.dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM EC"));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				Ec retour = new Ec();
				retour.setNumec(rs.getInt("NUMEC"));
				retour.setLibelle(rs.getString("LIBELLE"));
				retour.setCoef(rs.getDouble("COEF"));
				
				DAOUe daoUe = new DAOUeImpl();
				Ue ue = daoUe.find(rs.getInt("NUMUE"));
				retour.setUe(ue);
				
				retour.setCreneaux(null);
				retour.setJntexamenpromotionecs(null);
				retour.setJnttypeecenseignants(null);
				retour.setJnttypeecs(null);
				
				Lstretour.add(retour);
			}		
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return Lstretour;
	}

	@Override
	public void update(Ec o) {
		//TODO UPDATE RECURSIF ?
		try {
			this.dblink.connexion();
			String r = "UPDATE EC SET NUMUE="+o.getUe().getNumue()+", LIBELLE='"+o.getLibelle()+ "', COEF="+o.getCoef()+" WHERE NUMEC="+o.getNumec();
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

	@Override
	public void delete(Ec o) {
		//TODO DELETE RECURSIF ?
		try {
			this.dblink.connexion();
			String r = "DELETE FROM EC WHERE NUMEC="+o.getNumec();
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

}
